3 * Copyright © 2014 Wikimedia Foundation and contributors
5 * Heavily based on ApiQueryDeletedrevs,
6 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 use MediaWiki\MediaWikiServices
;
27 use MediaWiki\Revision\RevisionRecord
;
28 use MediaWiki\Storage\NameTableAccessException
;
31 * Query module to enumerate deleted revisions for pages.
35 class ApiQueryDeletedRevisions
extends ApiQueryRevisionsBase
{
37 public function __construct( ApiQuery
$query, $moduleName ) {
38 parent
::__construct( $query, $moduleName, 'drv' );
41 protected function run( ApiPageSet
$resultPageSet = null ) {
42 $user = $this->getUser();
43 // Before doing anything at all, let's check permissions
44 $this->checkUserRightsAny( 'deletedhistory' );
46 $pageSet = $this->getPageSet();
47 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
48 $pageCount = count( $pageSet->getGoodAndMissingTitles() );
49 $revCount = $pageSet->getRevisionCount();
50 if ( $revCount === 0 && $pageCount === 0 ) {
54 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
55 // Nothing to do, revisions were supplied but none are deleted
59 $params = $this->extractRequestParams( false );
62 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
64 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
66 if ( $resultPageSet === null ) {
67 $this->parseParameters( $params );
68 $arQuery = $revisionStore->getArchiveQueryInfo();
69 $this->addTables( $arQuery['tables'] );
70 $this->addFields( $arQuery['fields'] );
71 $this->addJoinConds( $arQuery['joins'] );
72 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
74 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
75 $this->addTables( 'archive' );
76 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
79 if ( $this->fld_tags
) {
80 $this->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'archive' ) ] );
83 if ( !is_null( $params['tag'] ) ) {
84 $this->addTables( 'change_tag' );
86 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
88 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
90 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
91 } catch ( NameTableAccessException
$exception ) {
93 $this->addWhere( '1=0' );
97 // This means stricter restrictions
98 if ( $this->fetchContent
) {
99 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
102 $dir = $params['dir'];
104 if ( $revCount !== 0 ) {
106 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
109 // We need a custom WHERE clause that matches all titles.
110 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
111 $where = $lb->constructSet( 'ar', $db );
112 $this->addWhere( $where );
115 if ( !is_null( $params['user'] ) ) {
116 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
117 $actorQuery = ActorMigration
::newMigration()
118 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
119 $this->addTables( $actorQuery['tables'] );
120 $this->addJoinConds( $actorQuery['joins'] );
121 $this->addWhere( $actorQuery['conds'] );
122 } elseif ( !is_null( $params['excludeuser'] ) ) {
123 // Here there's no chance of using ar_usertext_timestamp.
124 $actorQuery = ActorMigration
::newMigration()
125 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
126 $this->addTables( $actorQuery['tables'] );
127 $this->addJoinConds( $actorQuery['joins'] );
128 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
131 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
132 // Paranoia: avoid brute force searches (T19342)
133 // (shouldn't be able to get here without 'deletedhistory', but
134 // check it again just in case)
135 if ( !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' ) ) {
136 $bitmask = RevisionRecord
::DELETED_USER
;
137 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
138 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
143 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
147 if ( !is_null( $params['continue'] ) ) {
148 $cont = explode( '|', $params['continue'] );
149 $op = ( $dir == 'newer' ?
'>' : '<' );
150 if ( $revCount !== 0 ) {
151 $this->dieContinueUsageIf( count( $cont ) != 2 );
152 $rev = (int)$cont[0];
153 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
154 $ar_id = (int)$cont[1];
155 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
156 $this->addWhere( "ar_rev_id $op $rev OR " .
157 "(ar_rev_id = $rev AND " .
158 "ar_id $op= $ar_id)" );
160 $this->dieContinueUsageIf( count( $cont ) != 4 );
162 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
163 $title = $db->addQuotes( $cont[1] );
164 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
165 $ar_id = (int)$cont[3];
166 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
167 $this->addWhere( "ar_namespace $op $ns OR " .
168 "(ar_namespace = $ns AND " .
169 "(ar_title $op $title OR " .
170 "(ar_title = $title AND " .
171 "(ar_timestamp $op $ts OR " .
172 "(ar_timestamp = $ts AND " .
173 "ar_id $op= $ar_id)))))" );
177 $this->addOption( 'LIMIT', $this->limit +
1 );
179 if ( $revCount !== 0 ) {
180 // Sort by ar_rev_id when querying by ar_rev_id
181 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
183 // Sort by ns and title in the same order as timestamp for efficiency
184 // But only when not already unique in the query
185 if ( count( $pageMap ) > 1 ) {
186 $this->addWhereRange( 'ar_namespace', $dir, null, null );
188 $oneTitle = key( reset( $pageMap ) );
189 foreach ( $pageMap as $pages ) {
190 if ( count( $pages ) > 1 ||
key( $pages ) !== $oneTitle ) {
191 $this->addWhereRange( 'ar_title', $dir, null, null );
195 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
197 // Include in ORDER BY for uniqueness
198 $this->addWhereRange( 'ar_id', $dir, null, null );
200 $res = $this->select( __METHOD__
);
203 foreach ( $res as $row ) {
204 if ( ++
$count > $this->limit
) {
206 $this->setContinueEnumParameter( 'continue',
208 ?
"$row->ar_rev_id|$row->ar_id"
209 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
214 if ( $resultPageSet !== null ) {
215 $generated[] = $row->ar_rev_id
;
217 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
219 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
220 $converted = $pageSet->getConvertedTitles();
221 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
222 $title = Title
::newFromText( $converted[$title->getPrefixedText()] );
223 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
224 $pageMap[$row->ar_namespace
][$row->ar_title
] =
225 $pageMap[$title->getNamespace()][$title->getDBkey()];
229 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
232 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
236 $fit = $this->addPageSubItem(
237 $pageMap[$row->ar_namespace
][$row->ar_title
],
238 $this->extractRevisionInfo( $revisionStore->newRevisionFromArchiveRow( $row ), $row ),
242 $this->setContinueEnumParameter( 'continue',
244 ?
"$row->ar_rev_id|$row->ar_id"
245 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
252 if ( $resultPageSet !== null ) {
253 $resultPageSet->populateFromRevisionIDs( $generated );
257 public function getAllowedParams() {
258 return parent
::getAllowedParams() +
[
260 ApiBase
::PARAM_TYPE
=> 'timestamp',
263 ApiBase
::PARAM_TYPE
=> 'timestamp',
266 ApiBase
::PARAM_TYPE
=> [
270 ApiBase
::PARAM_DFLT
=> 'older',
271 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
275 ApiBase
::PARAM_TYPE
=> 'user'
278 ApiBase
::PARAM_TYPE
=> 'user'
281 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
286 protected function getExamplesMessages() {
288 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
289 'drvslots=*&drvprop=user|comment|content'
290 => 'apihelp-query+deletedrevisions-example-titles',
291 'action=query&prop=deletedrevisions&revids=123456'
292 => 'apihelp-query+deletedrevisions-example-revids',
296 public function getHelpUrls() {
297 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';